import 'package:citiesocial/_include.dart';

///
///单项菜单，用于目录、我的 等页面的单个项目
///
class ItemMenu extends StatelessWidget {
  final String text, leftIcon, rightIcon, subText;
  final double textSize, subTextSize, leftIconEdge, rightIconEdge;
  final Color textColor, subTextColor;
  final GestureTapCallback onTap;
  final Color bottomLine;
  final double bottomLineHeight;
  final double leftMargin, rightMargin;

  ItemMenu(
    this.text, {
    this.leftIcon,
    this.rightIcon,
    this.subText,
    this.textSize: 18.0,
    this.subTextSize: 15.0,
    this.leftIconEdge = 20.0,
    this.rightIconEdge = 18.0,
    this.textColor: Colors.black,
    this.subTextColor: Colors.grey,
    this.onTap,
    this.bottomLine = Configs.lineColor,
    this.bottomLineHeight = Configs.lineHeight,
    this.leftMargin = 10.0,
    this.rightMargin = 10.0,
  });

  List<Widget> _generateChildren() {
    var result = List<Widget>();
    if (leftIcon != null && leftIcon.length > 0)
      result.add(Container(
        child: '$leftIcon'.startsWith('imgs')
            ? Image.asset(
                '$leftIcon',
                width: leftIconEdge,
                height: leftIconEdge,
                fit: BoxFit.contain,
              )
            : Image(
                image: CachedNetworkImageProvider('$leftIcon'),
                width: leftIconEdge,
                height: leftIconEdge,
                fit: BoxFit.contain,
              ),
        margin: EdgeInsets.only(left: leftMargin),
      ));

    result.add(Container(
      child: Text(
        text,
        style: TextStyle(fontSize: textSize, color: textColor),
      ),
      margin: EdgeInsets.only(left: leftMargin),
    ));

    if (subText != null && subText.length > 0)
      result.add(Text(
        subText,
        style: TextStyle(fontSize: subTextSize, color: subTextColor),
      ));

    if (rightIcon != null && rightIcon.length > 0)
      result.add(Container(
        child: Image.asset(
          rightIcon,
          width: rightIconEdge,
          height: rightIconEdge,
          fit: BoxFit.contain,
        ),
        margin: EdgeInsets.symmetric(horizontal: rightMargin),
      ));
    return result;
  }

  Widget _item() => Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            color: Colors.white,
            child: Row(
              children: _generateChildren(),
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
            ),
            padding: EdgeInsets.symmetric(vertical: 12.0),
            margin: EdgeInsets.only(bottom: 1.0),
          ),
          Divider(
            color: bottomLine,
            height: bottomLineHeight,
          ),
        ],
      );

  @override
  Widget build(BuildContext context) => onTap != null
      ? GestureDetector(
          child: _item(),
          onTap: onTap,
        )
      : _item();
}
